package com.itheima.dubbo.mongodb.impl;

import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.commons.pojo.mongo.RecommendUser;
import com.itheima.commons.vo.reception.PageResult;
import com.itheima.dubbo.interfaces.RecommendUserApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author yaohu
 * @date 2021/12/6
 * @Version 1.0
 * @describe
 */
@Service
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * @param toUserId 要查询人的用户id
     * @return 缘分值最高的人
     */
    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        return mongoTemplate.findOne(
                Query.query(Criteria.where("toUserId").is(toUserId))
                        .with(Sort.by(Sort.Order.desc("score")))
                        .with(PageRequest.of(0, 1)),
                RecommendUser.class);
    }

    @Override
    public PageResult queryRecommendUserList(Integer page, Integer pagesize, Long toUserId) {
        List<RecommendUser> recommendUserList = mongoTemplate.find(
                Query.query(Criteria.where("toUserId").is(toUserId))
                        .with(Sort.by(Sort.Order.desc("score")))
                        .with(PageRequest.of(page - 1, pagesize)),
                RecommendUser.class);
        // 凡是涉及到分页的查询
        // 我们一般都要把分页相关参数都返回
        // 当前页码  每页显示的条数 每页的数据 总的页码 总的条数
        Long count = mongoTemplate.count(Query.query(Criteria.where("toUserId").is(toUserId)), RecommendUser.class);
        return new PageResult(page, pagesize, count, recommendUserList);
    }

    /**
     * 查询两个用户之间的缘分值
     *
     * @param toUserId 当前用户id
     * @param userId   要匹配的用户id
     * @return 缘分值
     */
    @Override
    public Long queryScoreByUserId(Long toUserId, Long userId) {
        RecommendUser recommendUser = mongoTemplate.findOne(Query.query(Criteria
                .where("toUserId").is(toUserId)
                .and("userId").is(userId)), RecommendUser.class);
        if (recommendUser != null) {
            return Convert.toLong(recommendUser.getScore());
        }
        return 0L;
    }

    /**
     * 探花功能卡片列表
     *
     * @param toUserId  给谁推荐的
     * @param counts    要查询的数量
     * @param exUserIds 排除用户ids
     * @return
     * @Param exUserId
     */
    @Override
    public List<RecommendUser> queryCardsList(Long toUserId, Integer counts, List<Long> exUserIds) {
        // 从mongoDB中随机抽样数据
        AggregationResults<RecommendUser> aggregate = mongoTemplate.aggregate(
                Aggregation.newAggregation(RecommendUser.class,
                        Aggregation.match(Criteria.where("toUserId").is(toUserId)),
                        Aggregation.match(Criteria.where("userId").nin(exUserIds)),
                        Aggregation.sample(counts)),
                RecommendUser.class);
        return aggregate.getMappedResults();
    }
}
